<?php
/**
 * 应用Smarty的Page类
 *
 * @package lib
 * @subpackage core.page
 * @author 苏宁 <snsnsky@gmail.com>
 *
 * $Id$
 */

namespace lib\plugins\page;

use lib\smarty\SmartyStart as Smarty;

class PageAdminSmarty extends APageFactory
{
    /**
     * Smarty 对象
     *
     * @var object Smarty
     * @access private
     */
    private $smarty;
    
    /**
     * 构造函数
     *
     * @param \lib\core\Application $app
     */
    public function __construct(& $app) 
    {
        parent::__construct($app);
        
        $this->smarty = new Smarty;

        // 设置模板目录
        $this->smarty->setTemplateDir(ADMIN_TPL_PATH);

        // 设置编译文件目录
        if (isset($app->cfg['smarty']['compile_dir']))
        {
            $this->smarty->setCompileDir($app->cfg['smarty']['compile_dir']);
        }
        
        // 设置插件目录
        $this->smarty->setPluginsDir(SMARTY_DIR . 'plugins/');
        
        // 赋值引用
        $this->smarty->assignByRef('cfg', $app->cfg);
        
        // 注册插件
        $this->smarty->registerPlugin('modifier', 'head', 'head');
        $this->smarty->registerPlugin('modifier', 'html', '_htmlspecialchars');
        $this->smarty->registerPlugin('modifier', 'date_custom', 'date_custom');

        // 设置默认的图片、样式、js、flash的模版路径
        $this->value('url_public', ADMIN_DOMAIN_ROOT . 'public/');
        $this->value('url_images', ADMIN_IMG);
        $this->value('url_css', ADMIN_CSS);
        $this->value('url_js', ADMIN_JS);

        // 设置站点名称
        $this->value('site_title', ADMIN_TITLE);
        $this->value('admin_ver', ADMIN_VER_INFO);
    }
    
    /**
     * 给页面变量赋值
     *
     * @param string $name 变量名,如果参数类型为数组,则为变量赋值,此时$value参数无效
     * @param mixed $value 变量值,如果该参数未指定,则返回变量值,否则设置变量值
     * @return PageSmarty 如果参数为NULL则返回Page对象本身,否则返回变量值
     */
    public function value($name, $value = NULL) 
    {
        //取值
        if ($value === NULL && !is_array($name))
        {
            return $this->smarty->getTemplateVars($name);
        }
        //赋值
        else
        {
            //如果是数组则批量变量赋值
            if (is_array($name))
            {
                foreach ($name as $k => $v)
                {
                    $this->smarty->assign($k, $v);
                }
            }
            else
            {
                $this->smarty->assign($name, $value);
            }
            return $this;
        }
    }
    
    
    /**
     * 页面内容输出
     * 
     * @param string    $template   指定输出的模板
     * @param boolean   $fetch      是否取回
     * @return string|void
     */
    public function output($template = '', $fetch = false)
    {
        if ($template)
        {
            $this->params['template'] = $template;
        }
        else if (!isset($this->params['template']))
        {
            $path = str_replace(ADMIN_MODULE_PATH, '', $this->app->cfg['path']['current']);
            $this->params['template'] = $path . $this->app->name . $this->app->module . '.tpl';
        }
        
        if ($fetch)
        {
            return $this->smarty->fetch($this->params['template']);
        }
        
        $this->smarty->display($this->params['template']);
    }
    
    /**
     * 返回smarty对象，供使用smarty其他功能
     */
    public function getSmarty()
    {
        return $this->smarty;
    }
}
